//
//  LJJSearchGoodsViewController.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/21.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJSearchGoodsViewController.h"
#import "LJJGoodsListViewController.h"
#import "LJJGoodsListModel.h"

@interface LJJSearchGoodsViewController ()<UISearchBarDelegate>

@property (strong, nonatomic) UISearchBar *searchBar;    /** 搜索框 **/

@end

@implementation LJJSearchGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.searchBar becomeFirstResponder];
}
#pragma mark -
#pragma mark - method
- (void)addNavigationItem
{
    self.navigationItem.leftBarButtonItem = nil;
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = item;
    //定义searchBar
    self.navigationItem.titleView = self.searchBar;
}
#pragma mark -
#pragma mark - searchBar delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self postData:@"appSearch/searchList.do" param:@{@"search":self.searchBar.text,@"OrderName":@"host",@"OrderType":@"ASC"} success:^(id responseObject) {
        NSArray *goodsListModel = [LJJGoodsListModel mj_objectArrayWithKeyValuesArray:responseObject];
        [searchBar resignFirstResponder];
        LJJGoodsListViewController *goodsList = [[LJJGoodsListViewController alloc] init];
        goodsList.goodsListArray = goodsListModel;
        goodsList.title = self.searchBar.text;
        [self.navigationController pushViewController:goodsList animated:YES];
    } error:^(NSError *error) {
        LJJLog(@"SearchResultError: --- %@", error);
    }];
}
#pragma mark -
#pragma mark - lazy method
- (UISearchBar *)searchBar
{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, 25)];
        _searchBar.delegate = self;
        _searchBar.showsCancelButton = YES;
        _searchBar.placeholder = @"商品名/功效/品牌";
    }
    return _searchBar;
}
@end
